﻿/*
 * Copyright (c) 2011 http://DesignBased.NET 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the 
 * Software  * without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
 * A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using T163.Open.Api;
using T163.Open.Api.Model;

namespace T163.Open.Api.WebSample
{
    public partial class _Default : System.Web.UI.Page
    {
        private OAuthService oauthService;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["OAuthService"] != null)
                oauthService = Session["OAuthService"] as OAuthService;

            if (Session["ScreenName"] != null)
            {
                ltlUser.Text = Session["ScreenName"].ToString();
                BtnLogin.Visible = false;
                HoemTimeLine();
            }

            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["oauth_token"]))
                {
                    try
                    {
                        oauthService.GetAccessToken(verifier: null);

                        Session["OAuthService"] = oauthService;

                        Users user = oauthService.VerifyCredentials();

                        Session["ScreenName"] = user.ScreenName;
                        BtnLogin.Visible = false;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                
                oauthService = new OAuthService("ConsumerKey", "ConsumerSecret");

                Session["OAuthService"] = oauthService;

                oauthService.GetRequestToken();

                Response.Redirect(oauthService.GetAuthorizationURL("http://localhost:3765/Default.aspx"));
            }
            catch (Exception ex)
            {
                txtMsg.Text += ex.Message + "\n\n"; 
            }
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (FileUpload1.HasFile)  // 图片微博
                {
                    byte[] image = FileUpload1.FileBytes;
                    string fileName = FileUpload1.FileName;
                    oauthService.Upload(fileName, image, txtStatues.Text);
                }
                else
                {
                    oauthService.Update(new Statues { Text = txtStatues.Text });                    
                }
                this.txtStatues.Text = "";

                HoemTimeLine();
            }
            catch (Exception ex)
            {
                txtMsg.Text += ex.Message + "\n\n";
            }
        }

        private void HoemTimeLine()
        {
            List<Statues> lst = oauthService.GetHomeTimeLine(10);

            gvStatues.DataSource = lst;
            gvStatues.DataBind();

            gvStatues.Visible = true;
        }
    }
}
